<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Project;
class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //项目名称
            if ($request->has('name') and $request->name != '') {
                $search = "%" . $request->name . "%";
                $query->where('name', 'like', $search);
            }
            if ($request->has('levy_scope') and $request->levy_scope != '') {
                $search = "%" . $request->levy_scope . "%";
                $query->where('levy_scope', 'like', $search);
            }
            if ($request->has('actuator') and $request->actuator != '') {
                $search = "%" . $request->actuator . "%";
                $query->where('actuator', 'like', $search);
            }



        };
        $allProject=Project::where($where)->orderBy('created_at', 'desc')->paginate(10);
        return view('admin.project.index',compact('allProject'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //新建项目
    public function createProject(Request $request){
        return view('admin.project.createProject');
    }

    //保存项目
    public function saveProject(Request $request){
        return  Project::newProject($request);
    }

    /*
    * 编辑项目
    * */
    public function editProject(Request $request){
        $arr=explode('=',$request->projectId);
        $projectId=$arr['1'];
        $editProject=Project::where('id',$projectId)->first();
        return view('admin.project.editProject',compact('editProject'));
    }

    /*
    * 修改项目
    * */
    public function updateProject(Request $request){
        return  Project::updateProject($request);
    }

    /*
 * 删除项目
 * */
    public function deleteProject(Request $request){
        $result=Project::destroy($request->projectId);
        if($result){
            $backMsg=['msg'=>200];
            return json_encode($backMsg);
        }
    }
}
